﻿/***************************************************************************************/
// Author                 - Balaji Venkatesan
// Student ID             - 12527079
// Version                - 1.0
// CodeName               - Northodyne Solutions
// Module                 - E-Business Technologies
// Date                   - 12-September-2011
/***************************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using Subgurim.Controls;
using Subgurim.Controles;

public partial class CustomerPropertyInformation : System.Web.UI.Page
{

    public string _connStr = "Initial Catalog=DBNorthodyneSolutions; Data Source=ABC\\SQLExpress;Trusted_Connection=Yes";
    public int returnValue = 0;
    public string _propertyID = "A1";
    public string _userName = string.Empty;
    public string resultantKey = string.Empty;
    public string _addressGMap = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        _userName = Session["UserName"].ToString();
        //lblUserName.Text = _userName;

        Label lblusername = (Label)Master.FindControl("lblUserName");
        HyperLink hyplogin = (HyperLink)Master.FindControl("hyplogin");
        lblusername.Text = _userName;

        if (!IsPostBack)
        {
            Session["CurrentID"] = _propertyID;

            using (SqlConnection connection = new SqlConnection(_connStr))
            {
                string selectPropertyInfo = "SELECT * from Tb_Property WHERE PropertyID = 'A1'";

                using (SqlCommand selectCommand = new SqlCommand(selectPropertyInfo, connection))
                {
                    connection.Open();

                    using (SqlDataReader propertyReader = selectCommand.ExecuteReader())
                    {

                        while (propertyReader.Read())
                        {
                            txtPropertyType.Text = propertyReader["PropertyType"].ToString();
                            txtTransactionType.Text = propertyReader["TransactionType"].ToString();
                            txtRooms.Text = propertyReader["Rooms"].ToString();
                            txtConcourse.Text = propertyReader["Concourse"].ToString();
                            txtSwimmingPool.Text = propertyReader["SwimmingPool"].ToString();
                            txtCost.Text = propertyReader["Cost"].ToString();
                            txtOwner.Text = propertyReader["Owner"].ToString();
                            txtContact.Text = propertyReader["AgentContact"].ToString();
                            txtAddress.Text = propertyReader["FullAddress"].ToString();
                            txtMRT.Text = propertyReader["NearestMRT"].ToString();
                            txtParking.Text = propertyReader["CarParking"].ToString();
                            txtGym.Text = propertyReader["Gym"].ToString();
                            txtPlayground.Text = propertyReader["Playground"].ToString();
                            txtAgentName.Text = propertyReader["AgentName"].ToString();
                            
                            _addressGMap = txtAddress.Text;
                        }
                    }
                }
                connection.Close();
            }
        }

        string sStreetAddress = "";
        string sMapKey = ConfigurationManager.AppSettings["googlemaps.subgurim.net"];
        GeoCode geocode;

        sStreetAddress = _addressGMap;
        geocode = GMap.geoCodeRequest(sStreetAddress, sMapKey);

        GLatLng gLng = new GLatLng(geocode.Placemark.coordinates.lat, geocode.Placemark.coordinates.lng);

        GMap1.setCenter(gLng, 16, Subgurim.Controles.GMapType.GTypes.Normal);
        GMarker oMarker = new GMarker(gLng);
        oMarker.options = new GMarkerOptions();
        GInfoWindow window = new GInfoWindow(gLng, "Location: " + '\n' + sStreetAddress);
        GMap1.addInfoWindow(window);
        oMarker.options.title = sStreetAddress;
        GMap1.addGMarker(oMarker);
        GMap1.mapType = GMapType.GTypes.Hybrid;
        GMap1.enableContinuousZoom = true;
        GMap1.enableHookMouseWheelToZoom = true;
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        string temp = Session["CurrentID"].ToString();
        int ActualPropertyID = Convert.ToInt32(temp.Substring(1));
        ActualPropertyID--;
        if (ActualPropertyID > 0)
        {
            resultantKey = "A" + ActualPropertyID;
            GetPropertyData(resultantKey);
            Session["CurrentID"] = resultantKey;
        }
    }

    protected void btnNext_Click(object sender, EventArgs e)
    {
        string temp = Session["CurrentID"].ToString();
        int ActualPropertyID = Convert.ToInt32(temp.Substring(1));
        ActualPropertyID++;

        SqlConnection connection = new SqlConnection(_connStr);
        connection.Open();
        string maxPropertyQuery = "SELECT max(PropertyID) from Tb_Property";
        SqlCommand maxCmd = new SqlCommand(maxPropertyQuery, connection);
        string maxPropertyIDValue = Convert.ToString(maxCmd.ExecuteScalar());
        int maxValue = Convert.ToInt32(maxPropertyIDValue.Substring(1));
        connection.Close();

        if (ActualPropertyID <= maxValue)
        {
            resultantKey = "A" + ActualPropertyID;
            GetPropertyData(resultantKey);
            Session["CurrentID"] = resultantKey;
        }
    }

    public void GetPropertyData(string pKey)
    {
        using (SqlConnection connection = new SqlConnection(_connStr))
        {
            connection.Open();
            string selectPropertyInfo = "SELECT * from Tb_Property WHERE PropertyID = @PropertyID";

            using (SqlCommand selectCommand = new SqlCommand(selectPropertyInfo, connection))
            {

                SqlParameter[] proprms = new SqlParameter[1];
                proprms[0] = new SqlParameter("@PropertyID", SqlDbType.NVarChar, 20);
                proprms[0].SqlValue = pKey;

                selectCommand.Parameters.AddRange(proprms);
                selectCommand.ExecuteNonQuery();

                using (SqlDataReader propertyReader = selectCommand.ExecuteReader())
                {
                    while (propertyReader.Read())
                    {
                        txtPropertyType.Text = propertyReader["PropertyType"].ToString();
                        txtTransactionType.Text = propertyReader["TransactionType"].ToString();
                        txtRooms.Text = propertyReader["Rooms"].ToString();
                        txtConcourse.Text = propertyReader["Concourse"].ToString();
                        txtSwimmingPool.Text = propertyReader["SwimmingPool"].ToString();
                        txtCost.Text = propertyReader["Cost"].ToString();
                        txtOwner.Text = propertyReader["Owner"].ToString();
                        txtContact.Text = propertyReader["AgentContact"].ToString();
                        txtAddress.Text = propertyReader["FullAddress"].ToString();
                        txtMRT.Text = propertyReader["NearestMRT"].ToString();
                        txtParking.Text = propertyReader["CarParking"].ToString();
                        txtGym.Text = propertyReader["Gym"].ToString();
                        txtPlayground.Text = propertyReader["Playground"].ToString();
                        txtAgentName.Text = propertyReader["AgentName"].ToString();

                        _addressGMap = txtAddress.Text;
                    }
                }
            }
            connection.Close();
        }

        string sStreetAddress = "";
        string sMapKey = ConfigurationManager.AppSettings["googlemaps.subgurim.net"];
        GeoCode geocode;

        sStreetAddress = _addressGMap;
        geocode = GMap.geoCodeRequest(sStreetAddress, sMapKey);

        GLatLng gLng = new GLatLng(geocode.Placemark.coordinates.lat, geocode.Placemark.coordinates.lng);

        GMap1.setCenter(gLng, 16, Subgurim.Controles.GMapType.GTypes.Normal);
        GMarker oMarker = new GMarker(gLng);
        oMarker.options = new GMarkerOptions();
        GInfoWindow window = new GInfoWindow(gLng, "Location: " + '\n' + sStreetAddress);
        GMap1.addInfoWindow(window);
        oMarker.options.title = sStreetAddress;
        GMap1.addGMarker(oMarker);
        GMap1.mapType = GMapType.GTypes.Hybrid;
        GMap1.enableContinuousZoom = true;
        GMap1.enableHookMouseWheelToZoom = true;
    }

    protected void  lnkbtnAddFavourites_Click(object sender, EventArgs e)
    {
        using (SqlConnection connection = new SqlConnection(_connStr))
        {
            connection.Open();
            string updatefavouriteInfo = "UPDATE Tb_Property SET Favourites = @Favourites WHERE AgentName = @AgentName";

            using (SqlCommand updatefavouriteCmd = new SqlCommand(updatefavouriteInfo, connection))
            {
                SqlParameter[] favouritePrms = new SqlParameter[2];
                favouritePrms[0] = new SqlParameter("@Favourites", SqlDbType.NVarChar, 50);
                favouritePrms[0].SqlValue = "true";

                favouritePrms[1] = new SqlParameter("@AgentName", SqlDbType.NVarChar, 50);
                favouritePrms[1].SqlValue = txtAgentName.Text.Trim();

                updatefavouriteCmd.Parameters.AddRange(favouritePrms);
                updatefavouriteCmd.ExecuteNonQuery();
            }
            connection.Close();
        }
        
    }

}